const puppeteer = require('puppeteer');
//将网页截图保存返回图片地址
module.exports = class extends think.Service {
	
	async create(url, origin) {
		console.log(url);
		const browser = await puppeteer.launch({
			args: ['--no-startup-window', '--no-first-run', '--skip-gpu-data-loading', '--incognito', '--no-sandbox', '--disable-setuid-sandbox'],
			timeout:100000
		});
		const page = await browser.newPage();
		await page.emulate({
			'name': 'Galaxy S5', //设备名
			'userAgent': 'Mozilla/5.0 (Linux; Android 5.0; SM-G900P Build/LRX21T) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%s Mobile Safari/537.36', //UA
			'viewport': {
				'width': 400, //屏幕宽度
				'height': 560, //屏幕高度
				'deviceScaleFactor': 3, //缩放比例
				'isMobile': true, //是否是移动设备
				'hasTouch': true, //是否支持touch事件
				'isLandscape': false //是否横屏
			}
		});
		await page.goto(url);
		let uuid = think.uuid("v1");
		let path = `${think.ROOT_PATH}/www/upload/snapshot/${uuid}.jpg`;
		await page.screenshot({
			path: path
		});
		await browser.close();
		return `${origin}/upload/snapshot/${uuid}.jpg`;
	}

}